﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Phone.Models
Imports Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel

Namespace Pages
    Partial Public Class SampleDataDetailsPage
        Inherits PhoneApplicationPage
        Public Sub New()
            Me.InitializeComponent()

            Dim viewModel = New SampleDataDetailsPageViewModel()
            AddHandler viewModel.SaveChangesSuccess, Sub(s, e) Me.NavigationService.GoBack()

            Me.DataContext = viewModel
        End Sub

        Protected Overrides Sub OnNavigatedTo(ByVal e As NavigationEventArgs)
            Dim viewModel = TryCast(Me.DataContext, SampleDataDetailsPageViewModel)
            If viewModel IsNot Nothing Then
                Dim editSampleDataAsString = Me.NavigationContext.QueryString("editSampleData")
                Dim editSampleData As Boolean

                If String.IsNullOrEmpty(editSampleDataAsString) OrElse (Not Boolean.TryParse(editSampleDataAsString, editSampleData)) Then
                    editSampleData = False
                End If

                If viewModel.SampleData Is Nothing Then
                    Dim sampleData = GetApplicationState(Of SampleData)("CurrentSampleDataRow")
                    If editSampleData AndAlso (sampleData Is Nothing) Then
                        Me.NavigationService.GoBack()
                    End If

                    viewModel.SetSampleDataModel(sampleData)
                    RemoveApplicationState("CurrentSampleDataRow")
                End If
            End If
        End Sub

        Private Shared Function GetApplicationState(Of T)(ByVal key As String) As T
            If Not PhoneApplicationService.Current.State.ContainsKey(key) Then
                Return Nothing
            End If

            Return CType(PhoneApplicationService.Current.State(key), T)
        End Function

        Private Shared Sub RemoveApplicationState(ByVal key As String)
            If PhoneApplicationService.Current.State.ContainsKey(key) Then
                PhoneApplicationService.Current.State.Remove(key)
            End If
        End Sub

        Private Sub OnTextBoxKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
            If e.Key = Key.Enter Then
                If sender Is Me.PartitionKeyTextBox Then
                    Me.RowKeyTextBox.Focus()
                ElseIf sender Is Me.RowKeyTextBox Then
                    Me.NameTextBox.Focus()
                ElseIf sender Is Me.NameTextBox Then
                    Me.DescriptionTextBox.Focus()
                ElseIf sender Is Me.DescriptionTextBox Then
                    Me.NumberTextBox.Focus()
                ElseIf sender Is Me.NumberTextBox Then
                    Me.DateDatePicker.Focus()
                Else
                    Me.PartitionKeyTextBox.Focus()
                End If
            End If
        End Sub

        Private Sub OnDeleteSampleData(ByVal sender As Object, ByVal e As EventArgs)
            Dim viewModel = TryCast(Me.DataContext, SampleDataDetailsPageViewModel)
            If viewModel IsNot Nothing Then
                viewModel.DeleteSampleData()
            End If
        End Sub

        Private Sub OnSaveSampleData(ByVal sender As Object, ByVal e As EventArgs)
            Dim viewModel = TryCast(Me.DataContext, SampleDataDetailsPageViewModel)
            If viewModel IsNot Nothing Then
                Dim focusTextbox = TryCast(FocusManager.GetFocusedElement(), TextBox)
                If focusTextbox IsNot Nothing Then
                    Dim binding = focusTextbox.GetBindingExpression(TextBox.TextProperty)
                    binding.UpdateSource()
                End If

                viewModel.SaveSampleData()
            End If
        End Sub
    End Class
End Namespace